﻿using System;
using System.Collections.Generic;
using System.Text;
using FluentData;
using System.Dynamic;

namespace Eleanor.DAL
{
    public class PosternTokenDAL : DALBase
    {
        public int Insert(dynamic posternToken)
        {
            return DefaultContext.Insert("postern_token")
                            .Column("start_time", (DateTime)posternToken.StartTime)
                            .Column("expired_time", (DateTime)posternToken.ExpiredTime)
                            .ExecuteReturnLastId<int>();
        }

        public dynamic CustomMapperUsingDynamic(dynamic row)
        {
            if (row == null)
            {
                return null;
            }
            dynamic posternToken = new ExpandoObject();
            posternToken.PosternTokenId = row.postern_token_id;
            posternToken.StartTime = row.start_time;
            posternToken.ExpiredTime = row.expired_time;
            posternToken.CreateDate = row.create_date;
            posternToken.UpdateDate = row.update_date;
            return posternToken;
        }

        public dynamic Select(int posternTokenId)
        {
            const string sql = @"select * from postern_token where postern_token_id=@0";
            return CustomMapperUsingDynamic(DefaultContext.Sql(sql).Parameters(posternTokenId).QuerySingle<dynamic>());
        }

        public List<dynamic> SelectAll()
        {
            string sql =@"select postern_token_id as PosternToken, start_time AS StartTime, expired_time as ExpiredTime 
                            from postern_token where start_time<=getdate() and expired_time>=getdate() order by postern_token_id desc";
            return DefaultContext.Sql(sql).QueryMany<dynamic>();
        }
    }
}